package com.qf.service.impl;

import com.qf.mapper.GradeMapper;
import com.qf.pojo.Grade;
import com.qf.service.GradeService;
import com.qf.utils.ResultMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GradeServiceImpl implements GradeService {
    @Autowired
    GradeMapper gradeMapper;

    /**
     * 全查所有年级
     * @return
     */
    @Override
    public ResultMsg selectAll(Integer page,Integer limit) {
        if (page!=null){
            page=page<=0?1:page;
        }
        List<Grade> grades=gradeMapper.selectAll(page==null?1:(page-1)*limit,limit);
        if (grades!=null){
            //查询所有行数，为分页准备，行数存在属性的ID里
            return new ResultMsg(0,"查询成功",grades,gradeMapper.selectAllRows().getGradeId());
        }else {
            return new ResultMsg(207,"查询失败",null,0);
        }
    }

    /**
     * 查询一个年级的信息
     * @param gradeId
     * @return
     */
    @Override
    public ResultMsg selectOne(Integer gradeId) {
       Grade grade = gradeMapper.selectOne(gradeId);
        if (grade!=null){
            return new ResultMsg(0,"查询成功",grade,1);
        }else {
            return new ResultMsg(208,"查询失败",null,0);
        }
    }

    /**
     * 修改一个年级信息
     * @param grade
     * @return
     */
    @Override
    public ResultMsg changeGrade(Grade grade) {
        int i=gradeMapper.changeGrade(grade);
        if (i==1){
            //success
            return new ResultMsg(0,"修改年级信息成功",null,0);
        }else {
            return ResultMsg.FAILED(209,"修改年级信息失败");
        }
    }

    /**
     * 新增一个年级
     * @param grade
     * @return
     */
    @Override
    public ResultMsg addGrade(Grade grade) {
       int i= gradeMapper.addGrade(grade);
        System.out.println(grade);
        if (i==1){
            //success
            return new ResultMsg(0,"添加年级成功",null,0);
        }else {
            return ResultMsg.FAILED(213,"添加年级失败");
        }
    }

    /**
     * 查询年级状态
     * @param gradeId
     * @return
     */
    public Integer selectGradeStatus(Integer gradeId){
       Grade grade = gradeMapper.selectGradeStatus(gradeId);
        return grade.getGradeStatus();
    }
    /**
     * 修改年级状态
     * @param gradeId
     * @return
     */
    @Override
    public ResultMsg changeGradeStatus(Integer gradeId) {
        Integer status = selectGradeStatus(gradeId);
        if (status==0){
            status=1;
        }else if (status==1){
            status=0;
        }
        int i = gradeMapper.changeGradeStatus(gradeId,status);
        if (i==1){
            //success
            return new ResultMsg(0,"修改年级状态成功",null,0);
        }else {
            return ResultMsg.FAILED(210,"修改年级状态失败");
        }
    }

    /**
     * 模糊查询（根据年级名称和状态）
     * @param name
     * @param status
     * @return
     */
    @Override
    public ResultMsg find(String name, String status) {
        Grade grade = new Grade();
        int cstatus=-1;
        if (status!=null&&!"".equals(status)){
            if (status.equals("0")){
                cstatus=0;
            }else if (status.equals("1")){
                cstatus=1;
            }
            grade.setGradeName(name);
            grade.setGradeStatus(cstatus);
        }else {
            grade.setGradeName(name);
        }
       List<Grade> grades = gradeMapper.find(grade);
        if (grades!=null){
            return new ResultMsg(0,"查询成功",grades,grades.size());
        }else {
            return new ResultMsg(212,"查询失败",null,0);
        }
    }

    /**
     * 批量删除年级
     * @param integers
     * @return
     */
    @Override
    public ResultMsg deleteGrade(List<Integer> integers) {
       int i = gradeMapper.deleteGrade(integers);
        if (i!=0){
            return new ResultMsg(0,"删除成功，删除了"+i+"个",null,0);
        }else {
            return new ResultMsg(211,"删除失败",null,0);
        }
    }
}
